import request from "../../utils/request";
import pubsub from 'pubsub-js';
import moment from 'moment';

const appData = getApp().globalData;
// pages/songDetail/songDetail.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        isPlay: false,
        song: {},
        musicId: '',
        musicLink: '',
        currentTime: '00:00',
        durationTime: '00:00',
        currentWidth: 0,
        modeId: 1,
        playMode: ['order', 'shuffle', 'repeat']
    },



    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

        let musicId = options.musicId;

        this.setData({
            musicId
        });

        this.getMusicInfo(musicId);

        if (appData.isMusicPlay && appData.musicId === musicId) {
            this.setData({
                isPlay: true,
            })
        }

        // 监视用户操纵系统控制音乐播放/暂停
        this.bgdAudioManager = wx.getBackgroundAudioManager();

        this.bgdAudioManager.onPlay(() => {
            this.changePlayState(true)
            // console.log(this.data.isPlay);
            appData.musicId = musicId;
        })

        this.bgdAudioManager.onPause(() => {
            this.changePlayState(false)
            // console.log(this.data.isPlay);
        })

        this.bgdAudioManager.onStop(() => {
            this.changePlayState(false)
            // console.log(this.data.isPlay);
        })

        this.bgdAudioManager.onTimeUpdate(() => {
            let currentTime = moment(this.bgdAudioManager.currentTime * 1000).format('mm:ss');

            let currentWidth = this.bgdAudioManager.currentTime / this.bgdAudioManager.duration * 450;

            this.setData({
                currentTime,
                currentWidth
            })
        })

        this.bgdAudioManager.onEnded(() => {
            pubsub.publish('switchType', {
                type: 'next',
                modeId: this.data.modeId
            });
            this.setData({
                currentWidth: 0,
                currentTime: '00:00'
            })
        })
    },

    switchMode() {
        let {
            modeId: modeIdData
        } = this.data;

        if (modeIdData === 2) {
            this.setData({
                modeId: 0
            })
        } else {
            this.setData({
                modeId: ++modeIdData
            })
        }
    },



    changePlayState(isPlay) {
        this.setData({
            isPlay
        })
        appData.isMusicPlay = isPlay;
    },

    async getMusicInfo(ids) {
        let {
            data: musicData
        } = await request('/song/detail', {
            ids
        });

        let durationTime = moment(musicData.songs[0].dt).format('mm:ss');

        this.setData({
            song: musicData.songs[0],
            durationTime
        })
    },

    playMusic() {
        this.setData({
            isPlay: !this.data.isPlay
        })
        // console.log(this.data.isPlay);

        this.musicControl(this.data.isPlay, this.data.musicId, this.data.musicLink);
    },

    async musicControl(isPlay, id, musicLink) {

        if (isPlay) {
            if (!musicLink) {
                let {
                    data: audioData
                } = await request('/song/url', {
                    id
                });
                musicLink = audioData.data[0].url;

                this.setData({
                    musicLink
                })
            }


            this.bgdAudioManager.src = musicLink;
            this.bgdAudioManager.title = this.data.song.name;
            this.bgdAudioManager.play();
        } else {
            this.bgdAudioManager.pause();
        }
    },

    handleSwitch(e) {
        let type = e.currentTarget.id;


        this.bgdAudioManager.stop();

        if (this.data.modeId == 2) {
            // console.log(musicId);
            this.getMusicInfo(this.data.musicId);
            this.musicControl(true, this.data.musicId);
            return;
        }
        pubsub.subscribe('musicId', (msg, musicId) => {
            // console.log(musicId);
            this.getMusicInfo(musicId);
            this.musicControl(true, musicId);
            pubsub.unsubscribe('musicId');
        })
        pubsub.publish('switchType', {
            type,
            modeId: this.data.modeId
        });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})